#include "RadarWidgit.h"
#include <QPainter>
#include <QTimerEvent>
#include <QConicalGradient>
#include <qDebug>

RadarWidgit::RadarWidgit(QWidget *parent) : QWidget(parent)
{
    //初始化
    m_backColor = QColor(30,30,30);
    m_drawColor = QColor(255,255,255);
    m_fanColor  = QColor(0,180,0);
    m_pieRotate = 0;
    m_timerId   = -1;
    m_nSpeed    = 50;
    m_points<<QPoint()<<QPoint()<<QPoint()<<QPoint()<<QPoint();

    //启动定时器
    m_timerId = startTimer(m_nSpeed);
    m_pointTimerId = startTimer(1200);
    connect(&fanTime,&QTimer::timeout,this,[=]()
    {
        m_pieRotate -= 5;
        update();
    });
    fanTime.start(m_nSpeed);
}

void RadarWidgit::setBackColor(QColor color)
{
    m_backColor = color;
}
void RadarWidgit::setDrawColor(QColor color)
{
    m_drawColor = color;
}
void RadarWidgit::setFanColor(QColor color)
{
    m_fanColor = color;
}
void RadarWidgit::setFanSpeed(int speed)
{
    m_nSpeed = speed;
    fanTime.setInterval(m_nSpeed);
}

void RadarWidgit::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);    //抗锯齿
    painter.fillRect(rect(),m_backColor);    //背景
    int len = m_drawArea.width();    //边长
    //底盘(x轴、y轴和3个圆)
    painter.setPen(QPen(m_drawColor));
    painter.drawLine(m_drawArea.topLeft() + QPoint(0,len/2),m_drawArea.topRight() + QPoint(0,len/2));
    painter.drawLine(m_drawArea.topLeft() + QPoint(len/2,0),m_drawArea.bottomLeft() + QPoint(len/2,0));
    painter.drawEllipse(m_drawArea.center(),len/2,len/2);
    painter.drawEllipse(m_drawArea.center(),len/3,len/3);
    painter.drawEllipse(m_drawArea.center(),len/6,len/6);
    //======================== 转动部分 =============================
    //---线
    qreal x = m_drawArea.center().x() + (qreal)len/2 * cos(-m_pieRotate*3.14159/180);
    qreal y = m_drawArea.center().y() + (qreal)len/2 * sin(-m_pieRotate*3.14159/180);
    painter.setPen(QPen(m_drawColor));
    painter.drawLine(m_drawArea.center(),QPointF(x,y));
    //---扇形
    QConicalGradient gradient;
    gradient.setCenter(m_drawArea.center());
    gradient.setAngle(m_pieRotate + 180); //渐变与旋转方向恰好相反，以扇形相反的边作为渐变角度。

    gradient.setColorAt(0.4,QColor(m_fanColor.red(),m_fanColor.green(),m_fanColor.blue(),255)); //从渐变角度开始0.5 - 0.75为扇形区域，由于Int类型计算不精确，将范围扩大到0.4-0.8
    gradient.setColorAt(0.8,QColor(m_fanColor.red(),m_fanColor.green(),m_fanColor.blue(),0));
    painter.setBrush(QBrush(gradient));
    painter.setPen(Qt::NoPen);
    painter.drawPie(m_drawArea,m_pieRotate*16,90*16);

    //============================装饰-随机点=======================
    for(int i = 0; i < m_points.count(); ++i)
    {
        painter.setPen(QPen(QColor(255,255,255),5));
        painter.drawPoint(m_points.at(i));
    }
}

void RadarWidgit::resizeEvent(QResizeEvent *event)
{
    //以较短的边长作为绘制区域边长
    if(width() > height())
        m_drawArea = QRect((width() - height())/2,0,height(),height());
    else
        m_drawArea = QRect(0,(height() - width())/2,width(),width());

    m_drawArea.adjust(10,10,-10,-10);
}

void RadarWidgit::timerEvent(QTimerEvent *event)
{
    if(m_pointTimerId == event->timerId())
    {
        //随机更换装饰的点
        for(int i = 0; i < m_points.count(); ++i)
        {
            int offsetX = rand()%m_drawArea.width();
            int offsetY = rand()%m_drawArea.width();
            int alapha = rand()%255;
            m_points.replace(i,QPoint(offsetX,offsetY) + m_drawArea.topLeft());
        }
        update();
    }
}


